package cn.swu.lzr.Safe;


import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;


public class Filter extends HttpFilter {
    private String[] ignoreUrls = null;

    public void init(FilterConfig filterConfig) throws ServletException
    {

        String urls = filterConfig.getInitParameter("IgnoreUrls");
        if (urls != null)
        {
            this.ignoreUrls = urls.split(";");

        }


    }

    public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        String uri = request.getRequestURI();
        for (String PermitUri : this.ignoreUrls)
        {

            if (uri.endsWith(PermitUri))
            {
                chain.doFilter(request,response);
                return;
            }
        }

        HttpSession session = request.getSession();
        String LoginStatus = (String) session.getAttribute("LoginStatus");
        if (LoginStatus != null && LoginStatus.equals("yes"))
        {
            chain.doFilter(request,response);
            return;
        }
        else {
            response.sendRedirect("/java-design/Log/login.html");
        }
    }

}
